import { createRouter, createMemoryHistory } from 'vue-router'
// import Home from '/@/components/Home.vue'
import About from '/@/components/About.vue'
import Video from '/@/components/Video.vue'
import Animate from '/@/components/Animate.vue'
import Login from '/@/components/Login.vue'
import UDP from '/@/components/UDP.vue'
import { store } from "./store";
import * as types from "./store/mutation-types";

const router = createRouter({
  history: createMemoryHistory(),
  routes: [
    {
      path: '/login',
      component: Login,
    },
    {
      path: '/video',
      component: Video
    },
    {
      path: '/animate',
      component: Animate
    },
    {
      path: '/udp',
      component: UDP
    },
  ]
});

router.beforeEach((to, from, next) => {
  let token = store.state.userToken;
  if (to.path === '/login') {
    if (token) {
      store.commit(types.SET_USER_TOKEN, '');
    }
    next();
  } else {

    if (token === 'null' || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

export default router;
